package com.example.demo.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.example.demo.service.baseContext.BaseContextService;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class ColumnFieldHandler implements MetaObjectHandler {
    @Autowired
    private BaseContextService bcs;

    @Override
    public void insertFill(MetaObject metaObject) {
        Date now = new Date();
        // this.strictInsertFill(metaObject, "createRole", String.class, "testrole");
        // this.strictInsertFill(metaObject, "updateRole", String.class, "testrole");
        this.strictInsertFill(metaObject, "createBy", String.class, bcs.getUserData().getUserId());
        this.strictInsertFill(metaObject, "updateBy", String.class, bcs.getUserData().getUserId());
        this.strictInsertFill(metaObject, "createTime", Date.class, now);
        this.strictInsertFill(metaObject, "updateTime", Date.class, now);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        Date now = new Date();
        // this.strictInsertFill(metaObject, "updateRole", String.class, "testrole");
        this.strictInsertFill(metaObject, "updateBy", String.class, bcs.getUserData().getUserId());
        this.strictInsertFill(metaObject, "updateTime", Date.class, now);
    }
}
